package com.hbm.usercenter.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @项目名: GooglePlay56
 * @包名: org.itheima56.googleplay.utils
 * @类名: CommonUtils
 * @创建者: 肖琦
 * @创建时间: 2015-5-9 下午6:00:40
 * @描述: TODO
 * @svn版本: $Rev: 39 $
 * @更新人: $Author: admin $
 * @更新时间: $Date: 2015-08-19 11:08:29 +0800 (星期三, 19 八月 2015) $
 * @更新描述: TODO
 */
public class CommonUtils {

    public static String getEtText(EditText et) {
        return et.getText().toString().trim();
    }

    /**
     * 格式化日期
     */
    //    public static String formatDate(String milliseconds) {
    //        long seconds = Long.parseLong(milliseconds);
    //        LogUtils.s(seconds+"---------呀呀呀");
    //        Calendar c = Calendar.getInstance();
    //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    //        String date = sdf.format(new Date(Long.valueOf(seconds+"000")));
    //        return date;
    //    }
    //    public static String timeStamp2Date(String seconds,String format) {
    //             if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
    //                     return "";
    //                 }
    //             if(format == null || format.isEmpty()){
    //                     format = "yyyy-MM-dd HH:mm:ss";
    //                 }
    //             SimpleDateFormat sdf = new SimpleDateFormat(format);
    //             return sdf.format(new Date(Long.valueOf(seconds+"000")));
    //         }
    public static String timestampToString(Integer time) {
        //int转long时，先进行转型再进行计算，否则会是计算结束后在转型
        long temp = (long) time * 1000;
        Timestamp ts = new Timestamp(temp);
        String tsStr = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //方法一
            tsStr = dateFormat.format(ts);
            System.out.println(tsStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tsStr;
    }

    /** 大陆号码*/
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 判断包是否安装
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isInstalled(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        try {
            manager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);

            return true;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 安装应用程序
     *
     * @param context
     * @param apkFile
     */
    public static void installApp(Context context, File apkFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 打开应用程序
     *
     * @param context
     * @param packageName
     */
    public static void openApp(Context context, String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        context.startActivity(intent);
    }

    public static void showShort(Context context, CharSequence message) {

        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
}
